/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.message;

import java.util.Date;

import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.cache.NewEventCache;
import com.sinodata.bsm.center.engine.event.EventWorkFlow;
import com.sinodata.bsm.center.service.event.EventService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.constants.EventConstants;
import com.sinodata.bsm.common.message.BsmActiveMQFactory;
import com.sinodata.bsm.common.vo.Event;

/**
 * <p>
 * Description: 集成处部事件消息的监听类
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-31 下午4:52:04          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EventMessageListener implements MessageListener {

    private static final Logger logger = Logger.getLogger("extEvent");

    private static EventMessageListener instance = null;
    private static EventService eventService = SpringContextHolder.getBean(EventService.class);
    private static NewEventCache newEventCache = SpringContextHolder.getBean(NewEventCache.class);

    private static final String eventQueueName = "ASY_EVT";
    private Session session;
    private Destination destination;
    private MessageConsumer consumer = null;
    private final int ackMode = Session.AUTO_ACKNOWLEDGE;

    private EventMessageListener() {
        try {
            session = BsmActiveMQFactory.getInstance().getSession(false, ackMode);
            destination = session.createQueue(eventQueueName);
            consumer = session.createConsumer(destination);
            consumer.setMessageListener(this);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public static synchronized void init() {
        if (instance == null) {
            instance = new EventMessageListener();
        }
    }

    @Override
    public void onMessage(Message message) {

        try {
            ObjectMessage objmsg = (ObjectMessage) message;
            Object msg = objmsg.getObject(); //待序列化的消息
            if (msg instanceof Event) {
                Event event = (Event) msg;
                logger.info("[TIP]\t  " + event.toString());
                if (event.getMsg() != null && event.getMsg().length() >= 2048) {
                    event.setMsg(event.getMsg().substring(0, 2048));
                }

                if (event.getOccurTime() == null) {
                    event.setOccurTime(new Date());
                }

                if (event.getStatus() == 0) { //新事件
                    Event sevent = newEventCache.getEventByObjectId(event.getObjectId(), event.getEventTypeId());
                    if (sevent == null) {
                        EventWorkFlow.getInstance().dispatch(event);
                    } else {//做事件压制
                        int cnt = sevent.getCount();
                        sevent.setCount(cnt + 1);
                        eventService.update(sevent);
                    }
                } else if (event.getStatus() == 2) {//恢复事件
                    eventService.outSideConfirm(event, EventConstants.CONFIRM_TYPE_AUTO, "System", "自动清除", "自动清除", true);
                }
            }
        } catch (Exception e) {
            logger.error("[ERROR]\t", e);
        }
    }
}
